from peewee import *


# 连接数据库
db = MySQLDatabase('world', user='yejing', password='881213', host='localhost', port=3306)

# 定义基类
class BaseModel(Model):
    class Meta:
        database = db

# 定义模型类
class User(BaseModel):
    id = PrimaryKeyField()
    name = CharField(max_length=255)
    age = IntegerField()

# 创建表
db.create_tables([User])

# 添加数据
new_user = User(name='John', age=50)
new_user.save()

# 查询数据
users = User.select()
for user in users:
    print(user.name, user.age)

# 关闭数据库连接
db.close()
